var form = layui.form, table = layui.table, $ = layui.jquery, id = getQueryVariable("id");
$('#editor-form').find('input,textarea,select').attr('disabled',true);

//产品明细数据
let detailList = [];
//实付明细
let actuallyList = [];
//退款明细
let refundList = [];
//附件明细数据
let annexList = [];
//操作日志数据
let operationLog = [];
layer.ready(function () {
    //获取详情信息
    $.ajax({
        url:"payment/detail?id="+id,
        success:function(res){
            if(res.status == 200){
                //主表信息赋值
                if(null != res.data){
                    //主表数据
                    let vo =  res.data;
                    form.val("editor-form", {
                        "billNo" : vo.billNo,
                        "time" : vo.time,
                        "supplierName" : vo.supplierName,
                        "bankName" : vo.bankName,
                        "bankAccount" : vo.bankAccount,
                        "typeStr" : vo.typeStr,
                        "currency" : vo.currency,
                        "usdRate" : vo.usdRate,
                        "rmbRate" : vo.rmbRate,
                        "amount" : vo.amount,
                        "companyName" : vo.companyName,
                        "operationNo" : vo.operation,
                        "createDate" : vo.createDate,
                        "memo" : vo.memo
                    });

                    //产品明细数据
                    if(null != res.data.detailList && res.data.detailList.length > 0){
                        detailList = res.data.detailList;
                    }
                    table.reload('maininformationtableid',{data : detailList});

                    //实付数据
                    if(null != res.data.actuallyList && res.data.actuallyList.length > 0){
                        actuallyList = res.data.actuallyList;
                    }
                    table.reload('realpaysubsidiarytableid',{data : actuallyList});

                    //退款明细
                    if(null != res.data.refundList && res.data.refundList.length > 0){
                        refundList = res.data.refundList;
                    }
                    table.reload('refunddetailtableid',{data : refundList});

                    //附件
                    if(null != res.data.annexList && res.data.annexList.length > 0){
                        annexList = res.data.annexList;
                    }
                    table.reload('attachmenttableid',{data : annexList});

                    //日志
                    if(!isEmpty(res.data.logList) && res.data.logList.length > 0){
                        operationLog = res.data.logList;
                    }
                    table.reload('logtableid',{data:operationLog});

                }
                return false;
            }else {
                layer.msg(res.msg, {icon: 5});
            }
        }
    });
})

/**
 * 获取是否含税
 * @param val
 * @returns {string}
 */
function getIsTaxIncluded(val){
    let res = "";
    if(isEmpty(val)){
        return res;
    }
    switch (val) {
        case 1:
            res = "含税出厂价";
            break;
        case 2:
            res = "未税出厂价";
            break;
        case 3:
            res = "含税FOB";
            break;
        case 4:
            res = "未税FOB价";
            break;
    }
    return res;
}

table.render({
    elem: '#maininformation',
    id: 'maininformationtableid',
    totalRow: true,
    cols: [[
        {field: 'numbers',width: 50,type:'numbers',title:'序号', totalRowText: '合计'},
        {field: 'invoiceNumber', width: 120, title: '发票号码', sort: true},
        {field: 'contractNo', width: 120, title: '外销合同号', sort: true},
        {field: 'purchaseNo', width: 120, title: '采购单号', sort: true},
        {field: 'typeName', width: 280, title: '付款类型'},
        {field: 'isTaxIncluded', width: 120, title: '是否含税', templet: function (data) {return getIsTaxIncluded(data["isTaxIncluded"]);}},
        {field: 'berPersonName', width: 180, title: '承担人员'},
        {field: 'amountsPayable', width: 120, title: '应付金额', sort: true, totalRow: true},
        {field: 'applicationAmount', width: 120, title: '申请金额', totalRow: true},
        {field: 'totalApplication', width: 120, title: '已申请合计', sort: true, totalRow: true},
        {field: 'claimAmount', width: 120, title: '索赔金额', totalRow: true},
        {field: 'allocatedAmount', width: 120, title: '已分摊金额', sort: true, totalRow: true},
        {field: 'unallocatedAmount', width: 120, title: '未分摊金额', sort: true, totalRow: true},
        {field: 'purchasePaidAmount', width: 120, title: '采购已付款金额', sort: true, totalRow: true},
        {field: 'totalPurchaseAmount', width: 120, title: '采购合同总额', sort: true, totalRow: true},
        {field: 'totalInvoiceAmount', width: 120, title: '发票合同总额', sort: true, totalRow: true},
        {field: 'invoicePaidAmount', width: 120, title: '发票已付款金额', sort: true, totalRow: true},
        {field: 'specialNote', width: 80, title: '特殊备注'},
        {field: 'carryDate', width: 120, title: '出运日期', sort: true},
        {field: 'customerCode', width: 120, title: '客户代码', sort: true},
        {field: 'saleAmountReceived', width: 120, title: '外销已收款金额', sort: true, totalRow: true},
        {field: 'supplierAccountPeriod', width: 120, title: '供应商账期', sort: true},
        {field: 'saleAccountPeriod', title: '外销客户账期', width: 120},
        {field: 'priceTermsName', title: '外销价格术语', width: 120}
    ]]
    ,page: false
    ,limit: Number.MAX_VALUE
    ,height: 380
    , done: function (res, curr, count) {
        $(".layui-table select").parent(".layui-table-cell").css('overflow','visible')
        $(".layui-form-select").parents(".layui-table-cell").css('overflow','visible')
    }
});

table.render({
    elem: '#realpaysubsidiary'
    ,totalRow: true
    ,cols: [[
        {field: 'numbers',width: 50,type:'numbers',title:'序号', totalRowText: '合计'},
        {field:'contractNo', title:'外销合同号',sort:true}
        ,{field:'invoiceNumber', title:'发票编号',sort:true}
        ,{field:'purchaseNo', title:'采购单号',sort:true}
        ,{field:'time', title:'实付日期',sort:true}
        ,{field:'bankName', title:'付款银行',sort:true}
        ,{field:'account', title:'付款账号',sort:true}
        ,{field:'amount', title:'实付金额',sort:true, totalRow: true}
        ,{field:'operator', title:'操作员名称'}
    ]]
    ,id: 'realpaysubsidiarytableid'
    ,height: 380
    ,page: false
    ,limit: Number.MAX_VALUE
});


table.render({
    elem: '#refunddetail',
    id: 'refunddetailtableid'
    ,totalRow: true
    ,cols: [[
        {field: 'numbers',width: 50,type:'numbers',title:'序号', totalRowText: '合计'},
        {field: 'invoiceNumber',  title: '发票编号',sort:true},
        {field: 'purchaseNo',  title: '采购合同号',sort:true},
        {field: 'contractNo',  title: '外销合同号',sort:true},
        {field: 'amount',  title: '退款金额',sort:true, totalRow: true},
        {field: 'memo',  title: '备注', sort:true}
    ]],
    height: 380
    ,page: false
    ,limit: Number.MAX_VALUE
});

//---附件
table.render({
    elem: '#attachment'
    , data: annexList
    , toolbar: '#attachmenttoolbar'
    , cols: [[
        {field: 'fileName',event:'fileName', title: '文件名', fixed: true}
        , {field: 'uploadTime', title: '上传日期', fixed: true}
        , {fixed: 'right', width: 180, align: 'center', toolbar: '#attachmenteditor', title: '操作'}
    ]]
    , id: 'attachmenttableid'
    , height: 380
    , page: true
});

table.on('tool(attachmenttable)', function (obj) {
    let data = obj.data;
    if (obj.event === 'download') { //下载
        let xmlHttpRequest = new XMLHttpRequest();
        let url = "wjinfo/downloadfile2?filename=" + data.filePath;
        xmlHttpRequest.open('GET', url, true);
        //设置请求头
        xmlHttpRequest.setRequestHeader("Content-type", "application/json")
        xmlHttpRequest.responseType = "blob";
        xmlHttpRequest.send(null);

        xmlHttpRequest.onload = function() {
            let content = xmlHttpRequest.response;
            // 组装a标签
            let elink = document.createElement("a");

            //设置文件下载路径
            elink.download = decodeURIComponent(data.fileName);
            elink.style.display = "none";
            let blob = new Blob([content], { type: 'application/force-download' });

            //解决下载不存在文件的问题，根据blob大小判断
            if(blob.size==0){
                layer.msg('服务器没找到此文件，请联系管理员!');
            }else{
                elink.href = window.URL.createObjectURL(blob);
                document.body.appendChild(elink);
                elink.click();
                document.body.removeChild(elink);
            }

        }
    }else if (obj.event === 'fileName'){
        onLineFile(data.filePath);
    }
});

//---操作日志
table.render({
    elem: '#otherwarehouselog'
    , data: operationLog
    , cols: [[
          {field: 'operationType', title: '操作类型', fixed: true}
        , {field: 'operationName', title: '操作员'}
        , {field: 'operationTime', title: '操作日期'}
        , {field: 'operationContent', title: '修改内容'}
    ]]
    , id: 'logtableid'
    , height: 380
    , limit: 30//显示的数量
    , page: true
});


//字段是否为空或者零
function isEmpty(obj){
    if(typeof obj == "undefined" || obj == null || obj == "" || obj == 0){
        return true;
    }else{
        return false;
    }
}